

const userModel = require('../lib/user.js');
const {checkNotLogin} = require('../middlewares/check.js')

exports.getLogin = async ctx => {
    await checkNotLogin(ctx)
    await ctx.render('login', {
            session: ctx.session,
    })
}
exports.postLogin = async ctx => {
  let { name,password } = ctx.request.body
  var result = await userModel.findDataByName(name)
  if(result.length && name === result[0]['name'] && password === result[0]['pass']){
    ctx.session = {
        user: result[0]['name'],
        id: result[0]['id']
    }
    ctx.body = {
        code: 200,
        message: '登录成功',
        data:{
            user: result[0]['name'],
            id: result[0]['id']
        }
    }
  }else{
    ctx.body = {
        code: 500,
        message: '用户名或密码错误'
    }
  }
    
}